package com.example;

import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;

import com.example.pages.InternalPage;
import com.example.pages.LoginPage;

import ru.esteru.selenium.factory.WebDriverFactory;

public class t02_changeEmailUser {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
private LoginPage LoginPage;
private InternalPage InternalPage;

  @Before
  public void setUp() throws Exception {
	driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());  
    //driver = new FirefoxDriver();
    baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void cahngeEmailUser() throws Exception {
    goToMainForm();
    loginAs("admin", "admin");
    gotoUserProfile();
    changeEmail("admin@admin.ru");
    logout();
  }

private void changeEmail(String newEmail) {
    driver.findElement(By.id("email")).clear();
    driver.findElement(By.id("email")).sendKeys(newEmail);
    driver.findElement(By.name("submit")).click();
}

private void gotoUserProfile() {
    driver.findElement(By.linkText("My profile")).click();
}

private void logout() {
	//driver.findElement(By.linkText("Log out")).click();
	InternalPage.menuLogoutLink.click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
}

private void loginAs(String username, String password) {
	//driver.findElement(By.id("username")).clear();
    //driver.findElement(By.id("username")).sendKeys(username);
    //driver.findElement(By.name("submit")).click();
    //driver.findElement(By.name("password")).clear();
    //driver.findElement(By.name("password")).sendKeys(password);
    //driver.findElement(By.name("submit")).click();
	
	LoginPage.userNameField.clear();
	LoginPage.userNameField.sendKeys(username);
	LoginPage.passwordField.clear();
	LoginPage.passwordField.sendKeys(password);
	LoginPage.loginButton.click();
    
}

private void goToMainForm() {
	//driver.get(baseUrl + "/");
    //driver.findElement(By.linkText("php4dvd")).click();
	driver.get(baseUrl + "/php4dvd/");
	LoginPage = PageFactory.initElements(driver, LoginPage.class);
	InternalPage = PageFactory.initElements(driver, InternalPage.class);
}

  @After
  public void tearDown() throws Exception {
    //driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}
